package quoridor;

public class QuoridorBoard implements Board
{
   private boolean[][] vertical;
   
   private boolean[][] horizontal;
   
   private Coordinate blackLocation;
   
   private Coordinate whiteLocation;
   
   private int blackFences;
   
   private int whiteFences;
   
   private int length;
   
   public QuoridorBoard(int newLength)
   {
      length = newLength;
      vertical = new boolean[length-1][length];
      horizontal = new boolean[length-1][length];
      blackLocation = new QuoridorCoordinate(length-1,length/2);
      whiteLocation = new QuoridorCoordinate(0,length/2);
   }
   
   @Override
   public Coordinate getPlayerLocation(String playerColour)
   {
      if (playerColour.equals("black"))
         return blackLocation;
      else if (playerColour.equals("white"))
         return whiteLocation;
      else
      {
         System.out.println("Playercolour not recognised - returning null.");
         return null;
      }
   }

   @Override
   public Move[] getMoves()
   {
      // TODO Auto-generated method stub
      return null;
   }

   @Override
   public boolean validateMove(Move newMove)
   {
      QuoridorMove move = (QuoridorMove) newMove;
      int type = move.getType();
      Coordinate dest = move.getCoordinate();
      switch (type)
      {
      // Move is changing the position of a piece
      // TODO - piece collisions
      case 0:
         {
            Coordinate src = this.getPlayerLocation(move.getPlayer());
            //Check if move is vertical
            if (src.getRow() == dest.getRow())
            {
               if ((src.getColumn() - dest.getColumn() == 1) &&
                     !vertical[dest.getColumn()][src.getRow()])
               {
                  return true;
               }
               else if ((src.getColumn() - dest.getColumn() == -1) &&
                     !vertical[src.getColumn()][src.getRow()])
               {
                  return true;
               }
            }
            //Check if move is horizontal
            else if (src.getColumn() == dest.getColumn())
            {
               if ((src.getRow() - dest.getRow() == 1) &&
                     !horizontal[dest.getRow()][src.getColumn()])
               {
                  return true;
               }
               else if ((src.getRow() - dest.getRow() == -1) &&
                     !horizontal[src.getRow()][src.getColumn()])
               {
                  return true;
               }
            }
            else if (src.getColumn() == dest.getColumn() && 
                  Math.abs(src.getRow()-dest.getRow()) == 1)
            {
               
            }
         }
      // Move is placing a fence
      case 1:
         {
            if (this.getPlayerFences(move.getPlayer()) < 1)
               return false;
            if (dest.getRow() > length-2 || dest.getColumn() > length-2)
               return false;
            // Check if the placing is vertical
            if (move.isVertical())
            {
               if (vertical[dest.getColumn()][dest.getRow()] || 
                     vertical[dest.getColumn()][dest.getRow()+1])
               {
                  return false;
               }
               else if (horizontal[dest.getRow()][dest.getColumn()] && 
                     horizontal[dest.getRow()][dest.getColumn()+1])
               {
                  return false;
               }
               // TODO Check whether a valid path will exist after the move
               else
                  return true;
            }
            // Check if the placing is horizontal
            else
            {
               if (horizontal[dest.getRow()][dest.getColumn()] ||
                     horizontal[dest.getRow()][dest.getColumn()+1])
               {
                  return false;
               }
               else if (vertical[dest.getColumn()][dest.getRow()] &&
                     vertical[dest.getColumn()][dest.getRow()+1])
               {
                  return false;
               }
               // TODO Check whether a valid path will exist after the move
               else
                  return true;
            }
         }
      }
      return false;
   }
   
   public int getPlayerFences(String playerColour)
   {
      if (playerColour.equals("black"))
         return blackFences;
      else if (playerColour.equals("white"))
         return whiteFences;
      else
      {
         System.out.println("Playercolour not recognised - returning null.");
         return -1;
      }
   }

}
